package com.sh.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.sh.common.core.enums.UserFoundEnum;
import com.sh.common.core.utils.MapstructUtils;
import com.sh.common.core.utils.StringUtils;
import com.sh.common.mybatis.core.page.TableDataInfo;
import com.sh.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sh.business.domain.bo.PriceTrendsBo;
import com.sh.business.domain.vo.PriceTrendsVo;
import com.sh.business.domain.PriceTrends;
import com.sh.business.mapper.PriceTrendsMapper;
import com.sh.business.service.IPriceTrendsService;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 价格趋势Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-14
 */
@RequiredArgsConstructor
@Service
public class PriceTrendsServiceImpl implements IPriceTrendsService {

    private final PriceTrendsMapper baseMapper;

    /**
     * 查询价格趋势
     *
     * @param id 主键
     * @return 价格趋势
     */
    @Override
    public PriceTrendsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询价格趋势列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 价格趋势分页列表
     */
    @Override
    public TableDataInfo<PriceTrendsVo> queryPageList(PriceTrendsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PriceTrends> lqw = buildQueryWrapper(bo);
        Page<PriceTrendsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<PriceTrendsVo> queryPortalPageList(PriceTrendsBo bo, PageQuery pageQuery) {
        pageQuery.setPageSize(5);

        bo.setType(UserFoundEnum.AVAILABLE_SZPN.getCode());
        LambdaQueryWrapper<PriceTrends> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(PriceTrends::getRealTime);

        Page<PriceTrendsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        // 翻转顺序
        if (CollUtil.isNotEmpty(result.getRecords())) {
            Collections.reverse(result.getRecords());
        }

        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的价格趋势列表
     *
     * @param bo 查询条件
     * @return 价格趋势列表
     */
    @Override
    public List<PriceTrendsVo> queryList(PriceTrendsBo bo) {
        LambdaQueryWrapper<PriceTrends> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PriceTrends> buildQueryWrapper(PriceTrendsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PriceTrends> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, PriceTrends::getType, bo.getType());
        lqw.eq(bo.getPrice() != null, PriceTrends::getPrice, bo.getPrice());
        lqw.eq(bo.getRealTime() != null, PriceTrends::getRealTime, bo.getRealTime());
        return lqw;
    }

    /**
     * 新增价格趋势
     *
     * @param bo 价格趋势
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PriceTrendsBo bo) {
        PriceTrends add = MapstructUtils.convert(bo, PriceTrends.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改价格趋势
     *
     * @param bo 价格趋势
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PriceTrendsBo bo) {
        PriceTrends update = MapstructUtils.convert(bo, PriceTrends.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PriceTrends entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除价格趋势信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
